﻿//===============================Stap3Algorithm.cs==============================\\
//===Peter Bonnema======\\




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace Team2_Vision
{
    class DecisionTree
    {
        public static List<char>[][, ,] alphabet = GetAlphabet();

        public DecisionTree() { }

        public static List<char>[][, ,] GetAlphabet()
        {
            //vul alfabet
            List<char>[][, ,] alphabet = new List<char>[6][, ,]
			{
				new List<char>[2, 2, 9],
				new List<char>[2, 2, 9],
				new List<char>[2, 2, 9],
				new List<char>[2, 2, 9],
				new List<char>[2, 2, 9],
				new List<char>[2, 2, 9],
			};

            for (int i = 0; i < alphabet.Length; i++)
            {
                alphabet[i].Initialize();
                for (int j = 0; j < 9; j++)
                {
                    alphabet[i][0, 0, j] = new List<char>();
                }
                for (int j = 0; j < 9; j++)
                {
                    alphabet[i][0, 1, j] = new List<char>();
                }
                for (int j = 0; j < 9; j++)
                {
                    alphabet[i][1, 0, j] = new List<char>();
                }
                for (int j = 0; j < 9; j++)
                {
                    alphabet[i][1, 1, j] = new List<char>();
                }
            }

            //lees patterns in
            string[] patterns = File.ReadAllLines("letters.csv");
            string[] entries, line;

            foreach (string pattern in patterns)
            {
                entries = pattern.Split('|');
                char letter = entries[0][0];
                for (int lineNumber = 1; lineNumber <= 6; lineNumber++)
                {
                    line = entries[lineNumber].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (!alphabet[lineNumber - 1][Int32.Parse(line[0]), Int32.Parse(line[1]), Int32.Parse(line[2]) - 1].Contains(letter))
                    {
                        //Console.WriteLine("letter: " + entries[lineNumber]);
                        alphabet[lineNumber - 1][Int32.Parse(line[0]), Int32.Parse(line[1]), Int32.Parse(line[2]) - 1].Add(letter);
                    }
                }
            }

            return alphabet;
        }

        public void InterpretPattern(int[][] pattern)
        {


            //List<char>[][, ,] alphabet = GetAlphabet();

            //IEnumerable<char> horizontal = alphabet[0][pattern[0][0], pattern[0][1], pattern[0][2] - 1],
            //                  vertical = alphabet[1][pattern[1][0], pattern[1][1], pattern[1][2] - 1],
            //                  diagonalLR = alphabet[2][pattern[2][0], pattern[2][1], pattern[2][2] - 1],
            //                  diagonalRL = alphabet[3][pattern[3][0], pattern[3][1], pattern[3][2] - 1];

            IEnumerable<char> horizontal1 = alphabet[0][pattern[0][0], pattern[0][1], pattern[0][2] - 1],
                              horizontal2 = alphabet[1][pattern[1][0], pattern[1][1], pattern[1][2] - 1],
                              horizontal3 = alphabet[2][pattern[2][0], pattern[2][1], pattern[2][2] - 1],
                              vertical1 = alphabet[3][pattern[3][0], pattern[3][1], pattern[3][2] - 1],
                              vertical2 = alphabet[4][pattern[4][0], pattern[4][1], pattern[4][2] - 1],
                              vertical3 = alphabet[5][pattern[5][0], pattern[5][1], pattern[5][2] - 1];

            List<char> results = new List<char>(1);

            //controleer of je nu iets hebt
            int equalRows, neededEqualRows = 6;
            while (results.Count == 0 & neededEqualRows > 3)
            {
                foreach (char letter in horizontal1)
                {
                    equalRows = 1;
                    if (horizontal2.Contains(letter))
                    {
                        equalRows++;
                    }
                    if (horizontal3.Contains(letter))
                    {
                        equalRows++;
                    }
                    if (vertical1.Contains(letter))
                    {
                        equalRows++;
                    }
                    if (vertical2.Contains(letter))
                    {
                        equalRows++;
                    }
                    if (vertical3.Contains(letter))
                    {
                        equalRows++;
                    }
                    if (equalRows >= neededEqualRows)
                    {
                        results.Add(letter);
                    }
                }
                neededEqualRows--;
            }

            char result = '?';
            for (int i = 1; i <= 3 & results.Count == 0; i += 2)
            {
                //concat huidige resultaat met arrays van -1 en +1 (als dat kan)

                horizontal1 = horizontal1.Concat(
                    alphabet[0][pattern[0][0], pattern[0][1], Math.Min(8, pattern[0][2] + i)]);

                horizontal2 = horizontal2.Concat(
                    alphabet[1][pattern[1][0], pattern[1][1], Math.Min(8, pattern[1][2] + i)]);

                horizontal3 = horizontal3.Concat(
                    alphabet[2][pattern[2][0], pattern[2][1], Math.Min(8, pattern[2][2] + i)]);

                vertical1 = vertical1.Concat(
                    alphabet[3][pattern[3][0], pattern[3][1], Math.Min(8, pattern[3][2] + i)]);

                vertical2 = vertical2.Concat(
                    alphabet[4][pattern[4][0], pattern[4][1], Math.Min(8, pattern[4][2] + i)]);

                vertical3 = vertical3.Concat(
                    alphabet[5][pattern[5][0], pattern[5][1], Math.Min(8, pattern[5][2] + i)]);


                horizontal1 = horizontal1.Concat(
                    alphabet[0][pattern[0][0], pattern[0][1], Math.Max(0, pattern[0][2] - i)]);

                horizontal2 = horizontal2.Concat(
                    alphabet[1][pattern[1][0], pattern[1][1], Math.Max(0, pattern[1][2] - i)]);

                horizontal3 = horizontal3.Concat(
                    alphabet[2][pattern[2][0], pattern[2][1], Math.Max(0, pattern[2][2] - i)]);

                vertical1 = vertical1.Concat(
                    alphabet[3][pattern[3][0], pattern[3][1], Math.Max(0, pattern[3][2] - i)]);

                vertical2 = vertical2.Concat(
                    alphabet[4][pattern[4][0], pattern[4][1], Math.Max(0, pattern[4][2] - i)]);

                vertical3 = vertical3.Concat(
                    alphabet[5][pattern[5][0], pattern[5][1], Math.Max(0, pattern[5][2] - i)]);

                //controleer of je nu iets hebt

                neededEqualRows = 6;
                while (results.Count == 0 & neededEqualRows > 3)
                {
                    foreach (char letter in horizontal1)
                    {
                        equalRows = 1;
                        if (horizontal2.Contains(letter))
                        {
                            equalRows++;
                        }
                        if (horizontal3.Contains(letter))
                        {
                            equalRows++;
                        }
                        if (vertical1.Contains(letter))
                        {
                            equalRows++;
                        }
                        if (vertical2.Contains(letter))
                        {
                            equalRows++;
                        }
                        if (vertical3.Contains(letter))
                        {
                            equalRows++;
                        }
                        if (equalRows >= neededEqualRows)
                        {
                            results.Add(letter);
                        }
                    }
                    neededEqualRows--;
                }
            }

            //Console.Write("Possible letters: ");
            //foreach (char letter in results)
            //{
            //    Console.Write(letter);
            //}
            //Console.WriteLine();

            if (results.Count > 0) result = results.First();

            //Console.WriteLine("Result: " + result);
            OCRForm.appendOutput(result);
        }
    }
}
